package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.BusinessItemBinding;
import sz.szsmk.citizencard.model.network.bean.response.PrepayResponse;
import sz.szsmk.citizencard.model.network.bean.response.ShopListRes;
import sz.szsmk.citizencard.ui.activity.TripPlanActivity;
import sz.szsmk.citizencard.util.GlideRoundTransform;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.StringUtils;
import sz.szsmk.citizencard.util.Utils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/27 13:42
 * description:
 */
public class BusinessAdapter extends BaseRecyclerAdapter<BusinessAdapter.BusinessHolder> {
    private Context mContext;
    private List<ShopListRes.ShopListInfosBean> itemData;
    private List<PrepayResponse.WebsiteListInfoBean> netData;
    private String key = "1";

    public BusinessAdapter(Context context) {
        mContext = context;
    }


    @Override
    public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return BusinessHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(BusinessHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(final int position, BusinessItemBinding binding) {
        if (itemData != null) {
            final ShopListRes.ShopListInfosBean infosBean = itemData.get(position);
            binding.businessAdress.setText(infosBean.getAddress());
            int i = infosBean.getDistance() / 10;
            String formatPrice = StringUtils.decimalFormatPrice(i);
            binding.businessDistance.setText(formatPrice + "km");

            Glide.with(mContext)
                    .load(infosBean.getPicUrl())
                    .error(R.drawable.zhanweitu)
                    .placeholder(R.drawable.zhanweitu)
                    .centerCrop()
                    .override(100, 100)
                    .transform(new GlideRoundTransform(mContext))
                    .into(binding.businessIv);
            binding.businessShop.setText(infosBean.getShopName());
            String telephone = infosBean.getTelephone();
            binding.bussnessTell.setText(telephone);
            if (telephone!=null){
                if (telephone.equals("")){
                    binding.bussnessTell.setText("");
                }else {
                    binding.bussnessTell.setText("联系商家");
                }
            }

            binding.bussnessTell.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + infosBean.getTelephone());
                    intent.setData(data);
                    mContext.startActivity(intent);
                }
            });

            binding.businessRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (key != null) {
                        if (key.equals("net")) {
                            double longitude = new Double(infosBean.getLongitude().toString());
                            double latitude = new Double(infosBean.getLatitude().toString());
                            Bundle bundle = new Bundle();
                            bundle.putInt("id", infosBean.getId());
                            bundle.putDouble("latitude", latitude);
                            bundle.putDouble("longitude", longitude);
                            JumpUtils.jumptoNatvie(mContext, "netdetail", 0, bundle);
                        } else {
                            ShopListRes.ShopListInfosBean shopListInfosBean = itemData.get(position);
                            Utils.startMap(mContext, shopListInfosBean);
                        }
                    } else {
                        ShopListRes.ShopListInfosBean shopListInfosBean = itemData.get(position);
//                        Utils.startMap(mContext, shopListInfosBean);
                        Utils.startMapBylocationByBaiduCorSys(mContext, (double) shopListInfosBean.getLatitude(),(double)shopListInfosBean.getLongitude(), shopListInfosBean.getShopName());
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == itemData ? 0 : itemData.size();
    }

    public void setItemData(List<ShopListRes.ShopListInfosBean> itemData, String door) {
        this.itemData = itemData;
        key = door;
        notifyDataSetChanged();
    }

    public void setNetData(List<PrepayResponse.WebsiteListInfoBean> netData) {
        this.netData = netData;
        notifyDataSetChanged();
    }

    public static class BusinessHolder extends BaseHolder {
        private BusinessItemBinding binding;


        public static BusinessHolder creat(ViewGroup parent) {
            BusinessItemBinding binding = BusinessItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new BusinessHolder(binding);
        }

        public BusinessHolder(BusinessItemBinding binding) {

            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
